/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import React, {ReactNode, useState, useEffect} from 'react';
import {View, StyleSheet, Dimensions, Image, Text, ScrollView} from 'react-native';
import {BreakpointsReference, GridCol, GridRow, GridRowDirection} from '@hadss/react_native_adaptive_layout/src';
import AvoidContainer from '../../components/AvoidContainer/AvoidContainer';

const Page = (): ReactNode => {
  const [width, setWidth] = useState(Dimensions.get('window').width);
  const blueHeight = 102;
  const itemList = [0, 1, 2, 3, 4, 5, 6, 7];
  const advertisementUri = require('../../../assets/common/advertisement.png');

  useEffect(() => {
    // 监听屏幕尺寸变化
    const subscription = Dimensions.addEventListener('change', (change) => {
      setWidth(change.window.width);
    });
    // 组件卸载时移除事件监听
    return (): void => {
      subscription.remove();
    };
  }, []);

  const renderItem = (): ReactNode => {
    const uri = require('../../../assets/common/startIcon.png');
    return (
      <View style={{ alignItems: 'center'}}>
        <Image source={uri} style={styles.img} resizeMode="cover" />
      </View>
    );
  };

  const renderItemB = (): ReactNode => {
    const uri = require('../../../assets/grid/background.jpg');
    return (
      <View style={{margin: 10}}>
        <Image source={uri} style={styles.imgB} resizeMode="cover" />
        <View style={{paddingTop: 5, paddingBottom: 5}}>
          <View style={styles.title}>
            <Text style={styles.titleText}>标题</Text>
          </View>
          <View style={styles.subTitle}>
            <Text style={styles.subTitleText}>副标题</Text>
          </View>
        </View>
      </View>
    );
  };

  return (
    <AvoidContainer>
      <ScrollView showVerticalScrollIndicator={false}>
        <View style={styles.container}>
          <GridRow
            style={styles.gridRow}
            columns={8}
            gutter={15}
            breakpoints={{
              reference: BreakpointsReference.ComponentSize,
            }}
            direction={GridRowDirection.Row}>
            {itemList.map((item, index) => (
              <GridCol
                span={{
                  xs: 2,
                  sm: 2,
                  md: 1,
                  lg: 1,
                  xl: 1,
                }}
                key={index}>
                {renderItem()}
              </GridCol>
            ))}
          </GridRow>
          <View
            style={[
              styles.blueArea,
              {width: width, height: blueHeight},
            ]}>
            <View style={styles.yellowArea} >
              <Text>float Ad</Text>
            </View>
            <Image
              source={advertisementUri}
              style={[
                styles.redArea,
                {
                  // 当宽度足够时显示浮动广告挂件
                  width: width > 440 ? 280 : '100%',
                  height: blueHeight,
                },
              ]}
            />
            <View style={styles.yellowArea} >
              <Text>float Ad</Text>
            </View>
          </View>
          <GridRow
            style={{marginTop: 15}}
            columns={6}
            breakpoints={{
              reference: BreakpointsReference.ComponentSize,
            }}
            direction={GridRowDirection.Row}>
            {itemList.map((item, index) => (
              <GridCol
                span={{
                  xs: 3,
                  sm: 3,
                  md: 2,
                  lg: 2,
                  xl: 2,
                }}
                key={index}>
                {renderItemB()}
              </GridCol>
            ))}
          </GridRow>
        </View>
      </ScrollView>
    </AvoidContainer>


  );
};

export default Page;

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  text: {
    fontSize: 16,
    marginBottom: 8,
    color: '#333',
  },
  textContainer: {
    padding: 20,
    backgroundColor: '#f0f0f0',
    borderRadius: 10,
  },
  blueArea: {
    flexDirection: 'row',
    justifyContent: 'center',
    backgroundColor: '#add8e6',
  },
  yellowArea: {
    width: 100,
    height: 100,
    marginTop: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#ecc6e4ff',
  },
  redArea: {
    marginLeft: 70,
    marginRight: 70,
  },
  img:{
    width: 50,
    height: 50,
  },
  gridRow: {
    marginTop: 50,
    marginBottom: 10,
  },
  imgB: {
    width: '100%',
    height: null,
    aspectRatio: 4 / 5,
    borderRadius: 12,
  },
    title: {
    width: '100%',
    marginBottom: 5,
    justifyContent: 'center',
    alignItems: 'center',
  },
  titleText: {
    color: '#000',
    fontSize: 16,
  },
  subTitle: {
    width: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
  subTitleText: {
    color: '#b0b0b0',
    fontSize: 12,
  },
});